const { ipcRenderer } = require('electron');
const WaveSurfer = require('wavesurfer.js');

let wave = WaveSurfer.create({
    container: '#waveform',
    waveColor: 'violet',
    progressColor: 'purple'
});

document.getElementById('fileInput').addEventListener('change', (event) => {
    const file = event.target.files[0];
    if (file) {
        wave.load(URL.createObjectURL(file));
    }
});

let splitPoints = [];
wave.on('click', (e) => {
    let time = wave.getCurrentTime();
    splitPoints.push(time);
    console.log(`Added split point: ${time}`);
    console.log(splitPoints)
});

document.getElementById('processAudio').addEventListener('click', () => {
    ipcRenderer.send('process-audio', { filePath: 'audio.mp3', splitPoints });
});


// 播放和暂停功能
document.getElementById('playPauseButton').addEventListener('click', () => {
    wave.playPause(); // 播放或暂停
});

// 停止音频播放
document.getElementById('stopButton').addEventListener('click', () => {
    wave.stop(); // 停止播放并重置到开头
});

// 实时显示播放时间和进度
wave.on('audioprocess', () => {
    if (wave.isPlaying()) {
        const currentTime = wave.getCurrentTime(); // 当前播放时间
        const duration = wave.getDuration(); // 总时长
        const progress = (currentTime / duration) * 100; // 播放进度百分比

        // 更新到页面
        document.getElementById('currentTime').innerText = `当前时间: ${currentTime.toFixed(2)} 秒 精确时间:${currentTime}`;
        document.getElementById('progress').innerText = `播放进度: ${progress.toFixed(2)}%`;
    }
});